package com.xbongbong.saas.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import java.util.ArrayList;
import java.util.List;

import com.xbongbong.saas.constant.StringConstant;
import lombok.Getter;

/**
 * @description: 最小考核周期枚举
 * @Author: hongxiao
 * @date: 2019-01-26 10:28
 * @since v1.0
 */
@Getter
public enum MinCycleEnum {
    /*
     * 最小考核周期枚举
     */
    MONTH(1, "month", "enum.minCycle.name.month"),
    WEEK(2, "week", "enum.minCycle.name.week"),
    DAY(3, "day", "enum.minCycle.name.day"),

    ;

    /** 枚举值 -- */
    private final int code;
    /** 枚举描述 --英文名称*/
    private final String	alias;
    /** 枚举信息--中文名称 */
    private final String    name;

    MinCycleEnum(int code, String alias, String name) {
        this.code = code;
        this.alias = alias;
        this.name = name;
    }

    /**
     * 获取全部枚举
     *
     * @return List<MinCycleEnum>
     */
    public static List<MinCycleEnum> getAllEnum() {
        List<MinCycleEnum> list = new ArrayList<>();
        for (MinCycleEnum cache : values()) {
            list.add(cache);
        }
        return list;
    }

    /**
     * 根据alias获取枚举
     * @return
     */
    public static MinCycleEnum getByCode(int code) {
        for (MinCycleEnum cache : values()) {
            if (code == cache.getCode()) {
                return cache;
            }
        }
        return null;
    }
    /**
     *
     * 封装最小考核周期选项
     * @param
     * @return JSONArray，封装最小考核周期各个选项
     * @throws
     *
     * @author hongxiao
     * @date: 2019-01-26 10:31
     * @since v1.0
     * @version v1.0
     */
    public static JSONArray getMinCycleArray() {
        JSONArray minCycleArray = new JSONArray();
        for (MinCycleEnum cache : values()) {
            JSONObject obj = new JSONObject();
            //最小考核周期code
            obj.put("minCycle", cache.code);
            //最小考核周期-名
            obj.put("name", cache.getName());
            minCycleArray.add(obj);
        }
        return minCycleArray;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }

    /**
     * 根据code匹配枚举，未匹配则返回"404"
     *
     * @param code
     * @return com.xbongbong.saas.enums.MinCycleEnum
     * @author zyy
     * @date 2022/3/31
     * @version v1.0
     */
    public static String getByCodeNotNull(int code) {
        MinCycleEnum minCycleEnum = getByCode(code);
        return minCycleEnum != null ? minCycleEnum.getName() : StringConstant.NOT_FIND;
    }
}
